<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
	<title>OS3 Grid Example 4 - Editable Grid</title>
	<link type="text/css" rel="stylesheet" href="css/examples.css" />
	<link type="text/css" rel="stylesheet" href="../os3grid.css" />
	<script src="../../../os3jslib/liwe.js" type="text/javascript"></script>
	<script src="../../../os3jslib/validators.js" type="text/javascript"></script>
	<script src="../os3grid.js" type="text/javascript"></script>
</head>
<body>
<div id="block_top">
	<h2 class="center">OS3Grid Example 4 - Editable Grid</h2>
	<h4 class="center">By Fabio Rotondo - fsoft (@) sourceforge ( dot ) net</h4>
</div>

<div id="block_left">
	<div class="menu_title">Examples</div>

	<div>
		<a href="index.html">Main Page</a><br />
		<a href="ex1.html">Simple Grid</a><br />
		<a href="ex2.html">Sortable Grid</a><br />
		<a href="ex3.html">Selectable Cells Grid</a><br />
		<a href="ex4.html">Editable Grid</a><br />
		<a href="ex5.html">Custom Callbacks</a><br />
		<a href="ex6.html">Rows Selection</a><br />
		<a href="ex7.html">Column Resize</a><br />
		<a href="ex8.html">Column Renderers</a><br />
		<a href="ex9.html">Delete row</a><br />
	</div>
	<div class="menu_title">Advanced Examples</div>
	<div>
		<a href="adv_styles.html">Setting and Using Styles</a><br />
		<a href="adv_edit.html">Using OS3Grid to edit a dataset</a><br />
		<a href="adv_cell_manip.html">Advanced Cell Value Manipulation</a><br />
	</div>

	<div class="menu_title">Support OS3Grid</div>
	<a href="http://sourceforge.net/donate/index.php?group_id=132180" target="_blank"><img src="http://images.sourceforge.net/images/project-support.jpg" width="88" height="32" border="0" alt="Support OS3Grid" /> </a>
</div>

<div id="block_middle">
	<div>
		Let's see now how to let the user edit our grid.<br />
		First of all, we have to set the columns that we want the user to be able to edit, this is done
		by the following commands:

		<div class="code"><pre>
				g.set_col_editable ( 1, "txt" );
				g.set_col_editable ( 2, "txt" );
				g.set_col_editable ( 3, "txt" );
		</pre></div>

		Since the last field should contain an e-mail, we can force the user to insert valid chars by
		filtering his/her typing against the given string and by adding a field validator:

		<div class="code"><pre>
				g.set_col_valid_chars ( 3, "abcdefghijklmnopqrstuvwxyz@.-0123456789_" );
				g.set_col_validation ( 3, check_email );
		</pre></div>

		The <tt>check_email</tt> is a form validation function defined inside <tt>form_validators.js</tt>.
		This file contains many more pre made validators, take a look at it and, if you are using it, do
		not forget to add the JavaScript file in your document with the following code:

		<div class="code"><pre>
			&lt;script src="form_validators.js" type="text/javascript"&gt;&lt;/script&gt;
		</pre></div>


		If we want the grid rows to be automatically resorted when the user changes grid contents, simply
		set this flag to true:	
	
		<div class="code"><pre>
				g.sort_on_edit = true;
		</pre></div>

		That's all! The final result is shown below.

		<div class="result">

			<div id="grid"></div>
			<script type="text/javascript">
				function val_changed ( grid, x, y, new_val )
				{
					alert ( "On grid: " + grid.id + " x: " + x + " y: " + y + " - value changed to: " + new_val );
				}

				// Create an OS3Grid instance
				var g = new OS3Grid ();

				// Grid Headers are the grid column names
				g.set_headers ( 'nick', 'Name', 'Surname', 'E-Mail Address' );

				// Now, we add some rows to the grid
				g.add_row ( 'fsoft', 'Fabio', 'Rotondo', 'fsoft (@) sourceforge (dot) net' );
				g.add_row ( 'john', 'John', 'Bustone', 'jbust (@) somewhere (dot) net' );
				g.add_row ( 'mkey', 'Mark', 'Key', 'mkey (@) okay (dot) net' );
				g.add_row ( 'jdoe', 'John', 'Doe', 'redbull (@) batman (dot) net' );

				// Enable sortable rows
				g.sortable = true;

				// Enable highlight of rows with the mouse
				g.highlight = true;

				g.set_col_editable ( 1, "txt" );
				g.set_col_editable ( 2, "txt" );
				g.set_col_editable ( 3, "txt" );

				g.onchange = val_changed;
				g.set_col_valid_chars ( 3, "abcdefghijklmnopqrstuvwxyz@.-0123456789_" );
				g.set_col_validation ( 3, check_email );

				g.set_row_select ( true );

				g.sort_on_edit = true;
				
				// Show the grid replacing the original HTML object with the "grid" ID.
				g.render ( 'grid' );
			</script>
		</div>
	</div>
</div>

</body>
</html>

